<template>
  <div class="home-search">
    <el-dialog v-model="visiable" :show-close="false" class="search-dialog" modal-class="search-modal" width="30%" :before-close="dialogClose">
      <template #header>
        <el-input v-model="inputValue" class="search-input" size="large" placeholder="搜索服务" :prefix-icon="Search" clearable @input="onInput" />
      </template>
      <div class="content">
        <div class="title">搜索结果</div>
        <el-scrollbar height="400px">
          <ul v-if="serverList.length > 0" class="search-result" v-loading="searchLoading">
            <li class="search-result-item" v-for="(item,index) in serverList" :key="index">
              <div class="logo">
                <img class="logo-img" :src="item.logo" alt="">
              </div>
              <div class="name">{{ item.name }}</div>
            </li>
          </ul>
          <el-empty v-else description="没有搜索"></el-empty>
        </el-scrollbar>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
/* eslint-disable */
import { computed, onBeforeUnmount, ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { getServerList } from '@/api/server'

const props = defineProps({
  visiable: {
    type: Boolean,
    default: false
  }
})
const emits = defineEmits(['update:visiable'])

const visiable = computed({
  get() {
    return props.visiable
  },
  set(val) {
    emits('update:visiable', val)
  }
})
// 搜索服务
const serverList = ref([])
const _getServerList = async () => {
  searchLoading.value = true
  const res = await getServerList()
  if(res.code === 20000){
    serverList.value = res.data
    serverList.value.forEach(item => {
      item.logo += Math.random() * 9999999
    })
  }
  searchLoading.value = false
}
const searchLoading = ref(false)
const inputValue = ref('')
const onInput = (val) => {
  _getServerList(val)
}
const dialogClose = () => {
  inputValue.value = ''
  serverList.value = []
  emits('update:visiable', false)
}
</script>

<style lang='scss' scoped>
::v-deep .el-dialog.search-dialog{
  background: #fafafa;
  .search-input{
    width: 100%;
    font-size: 16px;
    .el-input__wrapper{
      height: 56px;
    }
  }
  .content{
    .title{
      margin-bottom: 10px;
      font-size: 14px;
      font-weight: 600;
      color: #409EFF;
    }
    .search-result{
      padding: 0;
      .search-result-item{
        display: flex;
        flex-wrap: nowrap;
        align-items: center;
        overflow: hidden;
        padding: 0 12px;
        margin-bottom: 4px;
        width: 100%;
        height: 56px;
        border-radius: 4px;
        font-size: 14px;
        font-weight: 600;
        box-sizing: border-box;
        box-shadow: 0 1px 3px 0 #d4d9e1;
        background: #ffffff;
        .logo{
          margin-right: 10px;
          width: 40px;
          height: 40px;
          border-radius: 4px;
          overflow: hidden;
          img{
            width: 100%;
            height: 100%;
          }
        }
      }
      .search-result-item:hover{
        background: #409EFF;
        color: #ffffff;
        cursor: pointer;
      }
    }
  }
}
::v-deep .el-overlay.search-modal{
  background-color: rgba( 255, 255, 255 , .8);
}
</style>